<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="登录"
    >
      <!-- <van-icon
        slot="left"
        name="cross"
        @click="$router.back()"
      /> -->
    </van-nav-bar>
    <van-form ref="loginForm" @submit="onSubmit">
      <van-field
        v-model="user.account"
        name="account"
        placeholder="请输入6-9个字符的用户名"
        :rules="userFormRules.account"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.password"
        name="password"
        placeholder="请输入6-12个字符的密码"
        :rules="userFormRules.password"
        maxlength="12"
        type="password"
      >
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">
          学生登录
        </van-button>
        <van-button v-show='false' plain type="info" class="deng-btn" @click.stop="$router.push('/plogin')">去登陆&nbsp;&nbsp;&nbsp;&gt;&nbsp;&gt;</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
import { mapState } from 'vuex'
export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data () {
    return {
      user: {
        account: '',
        password: ''
      },
      userFormRules: {
        account: [{
          required: true,
          message: '用户名不能为空'
        }, {
          pattern: /^[a-zA-Z0-9_-]{6,11}$/,
          message: '用户名格式错误'
        }],
        password: [{
          required: true,
          message: '密码不能为空'
        }, {
          pattern: /^[a-zA-Z0-9_-]{6,12}$/,
          message: '密码格式错误'
        }]
      },
      isCountDownShow: false
    }
  },
  computed: {
    ...mapState(['id'])
  },
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onSubmit () {
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data } = await login(this.user)
        console.log(data, '000')
        if (data.code === 0) {
          this.$toast.fail('密码或账号错误')
        } else {
          this.$store.commit('setUser', data.data.userinfo.token)
          this.$store.commit('setId', data.data.userinfo.id)
          this.$toast.success('登录成功')
          this.$router.push('/user')
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }

  .send-sms-btn {
    width: 160px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }

  .login-btn-wrap {
    position: relative;
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
    .deng-btn {
      position: absolute;
      top: 170px;
      right: 30px;
      border: 0;
    }
  }
  .van-button--default {
    border: 0px;
  }
  .denglv {
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
}
</style>
